'use strict';
app.controller('functionCtrl',['$scope','utilService','functionService','toaster','$modal', function($scope, utilService, functionService,toaster,$modal){

    $scope.platform = 0;
    $scope.treeList = [];
    $scope.function = {};
    $scope.findByPlatformState=true;

    // call server.
    $scope.query = function(){
        $scope.findByPlatformState=true;
        // query data.
        functionService.findByPlatform($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.findByPlatformState=false;
                    var rootNode = result.data.children;
                    $scope.treeList = rootNode;
                    break;
                case 601:
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });
    }
    $scope.query();
    $scope.tree ={};
    $scope.disabled = false;
    $scope.selected=false;
    var existNewNode = false;

    $scope.treeOnSelect = function(branch){
        $scope.selected=true;
        var data = branch.data;
        $scope.function = data;
        $scope.disabled = data.id == ""?true:false;
    }

    $scope.createFunction = function(){
        var tree = $scope.tree;
        var branch = tree.get_selected_branch();
        var data = branch.data;

        if(data.id===''){
            toaster.pop('warning', "提示", "请保存当前节点");
        }else{
            existNewNode = true;
            tree.add_branch(branch,{
                label: 'New Function',
                data: {
                    id:"",
                    parentId:data.id,
                    code:"--",
                    name:"New Function",
                    icon:"",
                    url:"",
                    level:data.level + 1,
                    sort:data.childCount + 1,
                    childCount:0,
                    path:"--",
                    platform:$scope.platform
                }
            });
        }


    }

    $scope.saveFunction = function(){
        var id = $scope.function.id;
        if(id !== null && id !== undefined && id !== ""){
            // edit function
            edit();
        } else if (id == ""){
            // add function
            add();
        }else{
            toaster.pop('warning', "提示", "没有可操作的选择项。");
        }

        function add(){
            $scope.function.id = null;
            functionService.create($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.function = result.data;
                        $scope.query();
                        existNewNode = false;
                        toaster.pop('success', "提示", "操作成功");
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });

        }

        function edit(){
            $scope.function.platform = $scope.platform;
            functionService.edit($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.query();
                        toaster.pop('success', "提示", "操作成功");
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }
    }

    $scope.delete = function(){
        utilService.modal($modal, $scope, {
            title:"删除功能权限",
            controller:"deleteFunctionCtrl",
            message:"您是否要删除该功能",
            data:{
                id:$scope.function.id
            }
        });
    }


    //切换平台
    $scope.changePlatform=function(platform){
        $scope.selected=false;
        $scope.platform = platform;
        $scope.query();
    }

}]);

app.controller('deleteFunctionCtrl', ['$scope', '$modalInstance','$state','functionService','toaster','modalData',
    function($scope, $modalInstance,$state,functionService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.function = {
            id: modalData.data.id
        };
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            functionService.delete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);